package com.vint.nyist;

import java.util.Scanner;
import java.util.Stack;

/**
 * 2. 括号配对
 */
public class Nyist2 {
	public static void main(String args[]) throws Exception
	{
		Scanner cin=new Scanner(System.in);
		int team = Integer.parseInt(cin.nextLine());
		while(team-- > 0) {
			if (isMath(cin.nextLine())) {
				System.out.println("Yes");
			} else {
				System.out.println("No");
			}
		}
	}

	public static boolean isMath(String src) {
		if (null==src) return true;
		Stack<Character> stack = new Stack<>();
		for (char c : src.toCharArray()) {
			if (c == '(' || c == '[' || c == '{') {
				stack.add(c);
				continue;
			}
			if (c == ')') {
				if (stack.isEmpty()) {
					return false;
				}
				char top = stack.pop();
				if (top != '(') {
					return false;
				}
				continue;
			}
			if (c == ']') {
				if (stack.isEmpty()) {
					return false;
				}
				char top = stack.pop();
				if (top != '[') {
					return false;
				}
				continue;
			}
			if (c == '}') {
				if (stack.isEmpty()) {
					return false;
				}
				char top = stack.pop();
				if (top != '{') {
					return false;
				}
				continue;
			}
		}
		if (stack.isEmpty()) {
			return true;
		}
		return false;
	}
}
